package com.example.ketangpai_backend.utils;

import io.jsonwebtoken.*;

import java.util.Date;
import java.util.Map;
import java.util.UUID;

public class JwtUtil {

    //有效期
    private static long time=1000*60*60*8;
    //签名
    private static final String secretKey = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ"; //进行数字签名的私钥，一定要保管好

    public static String createToken(Map<String, Object> claims){
        JwtBuilder jwtBuilder = Jwts.builder();
        String jwtToken=jwtBuilder
                //header
                .setHeaderParam("typ","JWT")
                .setHeaderParam("alg","HS256")
                //payload
                .setId(UUID.randomUUID().toString())
                .setExpiration(new Date(System.currentTimeMillis()+time))
                //signature
                .signWith(SignatureAlgorithm.HS256,secretKey)
                .compact();
        return jwtToken;

    }
    /**
     * 解析JWT字符串
     */
    public static Claims parseJWT(String jwt) {
        return Jwts.parser()
                .setSigningKey(secretKey)
                .parseClaimsJws(jwt)
                .getBody();
    }
}
